//
//  NYGoodSubjectBookViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/3.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYGoodSubjectBookViewController.h"
#import "NYPageViewController.h"
#import "NYSubjectListRequest.h"
#import "NYSubjectTitleCell.h"
#import "NYSubjectAnswerCell.h"
#import "NYGoodSubjectActionCell.h"
#import "NYSubjectBookPageInfoCell.h"
#import "NYWrongSubjectBookActionCell.h"
#import "NYWrongAnswerTipCell.h"
#import "NYRefreshFooter.h"
#import "NYSubjectAnalysisTableViewController.h"
#import "NYSubjectAnalysisViewController.h"
#import "NYSubjectListViewController.h"

@interface NYGoodSubjectBookViewController ()<WGPageViewControllerDelegate>
@property(nonatomic, strong) NYPageViewController *pageViewController;
@property(nonatomic, strong) NYSubjectListModel *subjectList;
@property(nonatomic, strong) QMUIButton *topSortRightBtn;
@end

@implementation NYGoodSubjectBookViewController

- (void)viewDidLoad
{
        [super viewDidLoad];
        [self configPageViewController];
        [self loadData];
}
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar
{
        [super configNavigationBar:navigationBar];
        self.title = @"好题本";
}

- (void)loadData
{
        [self setLoading:YES animation:YES];
        [[NYSubjectListRequest requestWithType:(NYSubjectListTypeGoodSubject) params:nil] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYSubjectListModel * _Nonnull data) {
                self.subjectList = data;
                [self reloadPageViewController];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)reloadPageViewController
{
        NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:self.subjectList.category.count];
        [tmp addObject:@"全部"];
        [self.subjectList.category enumerateObjectsUsingBlock:^(NYSubjectCategoryModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [tmp addObject:obj.name];
        }];
        self.pageViewController.segmentTitles = tmp;
        [self.pageViewController reloadData];
}

/// MARK: - PageViewController Delegate
- (NSInteger)countOfContentItems
{
        return self.pageViewController.segmentTitles.count;
}
- (UIViewController *)contentControllerAtIndex:(NSInteger)index
{
        NYSubjectListViewController *vc = [[NYSubjectListViewController alloc] init];
        vc.category_id = [(NYSubjectCategoryModel *)[self.subjectList.category jk_objectWithIndex:index - 1] id];
        vc.subjectListType = NYSubjectListTypeGoodSubject;
        return vc;
}

- (void)configController:(NYSubjectListViewController *)controller atIndex:(NSInteger)index
{
        
}
- (void)controllerDidShow:(NYSubjectListViewController *)controller atIndex:(NSInteger)index
{
        
}


- (void)configTopSortView
{
        [self.pageViewController.topSortView addSubview:self.topSortRightBtn];
        [self.topSortRightBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.equalTo(self.pageViewController.topSortView);
                make.width.mas_equalTo(__WGWidth(40));
        }];
}
- (void)configPageViewController
{
        self.pageViewController = [[NYPageViewController alloc] init];
        [self.pageViewController willMoveToParentViewController:self];
//        self.pageViewController.scrollEnable = NO;
        [self addChildViewController:self.pageViewController];
        self.controllerView.contentView = self.pageViewController.view;
        [self.pageViewController didMoveToParentViewController:self];
        self.pageViewController.pageDelegate = self;
        self.pageViewController.topSortBottomLineColor = UIColorSeparator;
        self.pageViewController.segmentControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
        self.pageViewController.segmentControl.selectedTitleTextAttributes = @{
                                                                               NSForegroundColorAttributeName : UIColorHex(0x5396F7),
                                                                               NSFontAttributeName : WGSystem14Font,
                                                                               };
        self.pageViewController.segmentControl.titleTextAttributes = @{
                                                                       NSForegroundColorAttributeName : UIColorHex(0x333333),
                                                                       NSFontAttributeName : WGSystem14Font,
                                                                       };
        self.pageViewController.segmentControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, -3, 0, -3);
        self.pageViewController.segmentControl.selectionIndicatorColor = UIColorHex(0x5396F7);
        self.pageViewController.segmentControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
        self.pageViewController.segmentLayoutEdgeInsets = UIEdgeInsetsMake(0, 0, 0, __WGWidth(40));
        [self configTopSortView];
}

- (QMUIButton *)topSortRightBtn
{
        if (_topSortRightBtn == nil) {
                _topSortRightBtn = [[QMUIButton alloc] qmui_initWithImage:[UIImage wo_de_kao_shi_xiang_you_jian_tou] title:nil];
                _topSortRightBtn.qmui_borderPosition = QMUIBorderViewPositionLeft | QMUIBorderViewPositionBottom;
        }
        return _topSortRightBtn;
}

/// NavBar
+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}

@end
